package com.zito.health.api.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zito.health.api.annotation.AppGetMapping;
import com.zito.health.api.annotation.AppPostMapping;
import com.zito.health.api.result.ResponseBean;
import com.zito.health.common.result.PageResult;
import com.zito.health.common.result.VerifyResult;
import com.zito.health.entity.LaboratoryInspect;
import com.zito.health.service.LaboratoryInspectService;
import com.zito.health.vo.LaboratoryInspectVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/app/inspect")
@Slf4j
public class LaboratoryInspectAppController {
    @Autowired
    LaboratoryInspectService laboratoryInspectService;

    /**
     * 新增巡检和巡检图片
     * @param laboratoryInspect
     * @auth sun
     */
    @AppPostMapping("/saveInspect")
    public ResponseBean saveInspect(LaboratoryInspect laboratoryInspect) {
        VerifyResult verifyResult = new VerifyResult();
        laboratoryInspectService.saveInspect(laboratoryInspect);
        return new ResponseBean(200,verifyResult.getMsg(),verifyResult);
    }

    /**
     * 查询巡检
     * @auth sun
     */
    @AppGetMapping("/queryList")
    public ResponseBean queryList(@RequestParam(defaultValue = "0") Integer pageNo, @RequestParam(defaultValue = "10")Integer pageSize,LaboratoryInspect laboratoryInspect){
        PageHelper.startPage(pageNo,pageSize);
        List<LaboratoryInspectVo> laboratoryInspectList = laboratoryInspectService.queryList(laboratoryInspect);
        PageInfo<LaboratoryInspectVo> pageInfo = new PageInfo(laboratoryInspectList);
        PageResult<LaboratoryInspectVo> result = new PageResult();
        result.setRows(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());
        return new ResponseBean(200,"",result);
    }

    /**
     * 删除
     * @auth sun
     */
    @AppPostMapping("/delect")
    public ResponseBean delete(Integer id){
        VerifyResult verifyResult = new VerifyResult();
        laboratoryInspectService.deleteInspect(id);
        return new ResponseBean(200,verifyResult.getMsg(),verifyResult);
    }

    /**
     * 提交整改，修改巡检
     * @auth sun
     */
    @AppPostMapping("/update")
    public ResponseBean update(LaboratoryInspect laboratoryInspect){
        VerifyResult verifyResult = new VerifyResult();
        laboratoryInspect = laboratoryInspectService.queryById(laboratoryInspect.getId());
        laboratoryInspect.setCompletionPersonId(laboratoryInspect.getLaboratoryId());
        laboratoryInspect.setCompletionTime(new Date());
        laboratoryInspect.setStates((short) 1);
        laboratoryInspectService.update(laboratoryInspect);
        return new ResponseBean(200,verifyResult.getMsg(),verifyResult);
    }

    /**
     * 查询详情
     * @auth sun
     */
    @AppGetMapping("/queryInspectByid")
    public ResponseBean queryInspectByid(Integer id){
        LaboratoryInspectVo laboratoryInspectVo = laboratoryInspectService.queryInspectByid(id);
        String[] split = laboratoryInspectVo.getUrl().split(",");
        // 详情需要url数组
        laboratoryInspectVo.setUrlArr(split);
        // 如整改完成
        if (laboratoryInspectVo.getStates() != 0) {
            laboratoryInspectVo.setCompletionPersonName(laboratoryInspectVo.getLaboratoryNmae());
        }
        return new ResponseBean(200,"",laboratoryInspectVo);
    }

    /**
     * 审核
     * @auth sun
     */
    @AppPostMapping("/auditInspect")
    public ResponseBean auditInspect(LaboratoryInspect laboratoryInspect){
        VerifyResult verifyResult = new VerifyResult();
        laboratoryInspectService.update(laboratoryInspect);
        return new ResponseBean(200,verifyResult.getMsg(),verifyResult);
    }
}
